package com.wonders.common.parse.jaxb.config.scaner.instance;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import com.wonders.common.parse.jaxb.config.JaxbConfig;
import com.wonders.common.parse.jaxb.config.handler.XsdPackageHandler;
import com.wonders.common.parse.jaxb.config.scaner.BaseConfigScaner;
import com.wonders.common.ws.constant.XtptConstant;
import com.wonders.framework.util.web.ApplicationUtil;

/**
 * 协同平台配置查找者
 * 
 * @author WuBin
 * 
 */
public class XtptConfigScaner extends BaseConfigScaner {

	private Logger logger = LoggerFactory.getLogger(XtptConfigScaner.class);

	/**
	 * 版本
	 */
	private String version = "";

	public XtptConfigScaner(String version) {
		this.version = version;
	}

	@Override
	public void scanConfig(String xsdPath) {
		ClassPathResource pack = new ClassPathResource(xsdPath);
		File[] files = null;
		try {
			files = pack.getFile().listFiles();
		} catch (IOException e) {
			logger.error("自动获取JAXB配置异常", e);
			return;
		}
		for (File f : files) {
			if (!f.isDirectory()) {
				continue;
			}
			scanSubPackage(StringUtils.join(xsdPath, "/", f.getName()));
		}
	}

	private void scanSubPackage(String xsdPath) {
		ClassPathResource pack = new ClassPathResource(xsdPath);
		File[] files = null;
		try {
			files = pack.getFile().listFiles();
		} catch (IOException e) {
			logger.error("自动获取JAXB配置异常", e);
			return;
		}
		for (File f : files) {
			if (f.isDirectory() || !f.getName().endsWith(".xsd")) {
				continue;
			}
			String packageName = parsePackage(f);
			if (StringUtils.isEmpty(packageName)) {
				continue;
			}
			String key = StringUtils.join(xsdPath.substring(xsdPath
					.lastIndexOf("/") + 1), "_",
					f.getName().substring(0, f.getName().length() - 7));
			String type = f.getName().substring(f.getName().length() - 7,
					f.getName().length() - 4);
			String classPath = null;
			if (XtptConstant.VERSION_NEW.equals(version)) {
				classPath = StringUtils.join(packageName, ".Data");
			} else {
				classPath = StringUtils.join(packageName, ".TradeMsg");
			}
			String xsdFilePath = StringUtils.join(pack.getPath(), "/",
					f.getName());
			ApplicationUtil.getBean(JaxbConfig.class).addXsdConfig(key, type,
					classPath, xsdFilePath);
		}
	}

	/**
	 * 获取包名
	 * 
	 * @param xsd
	 *            xsd文件
	 * @return 包名
	 */
	private String parsePackage(File xsd) {
		if (xsd == null || !xsd.exists()) {
			return null;
		}
		SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
		XsdPackageHandler handler;
		try {
			SAXParser saxParser = saxParserFactory.newSAXParser();
			handler = new XsdPackageHandler();
			saxParser.parse(xsd, handler);
			return handler.getPackageName();
		} catch (Exception e) {
			logger.error("自动获取JAXB配置异常", e);
			return null;
		}
	}

}
